package net.chenyan.server.configure;

import net.chenyan.server.web.AdminServiceInterceptor;
import net.chenyan.server.web.LoginInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;

/**
 * web配置<br>
 * 用于注入拦截器
 *
 * @author tanghuang 2020年02月25日
 */
@Configuration
public class WebConfigurerAdapter extends WebMvcConfigurationSupport {

//    @Bean
//    AdminServiceInterceptor adminServiceInterceptor() {
//        return new AdminServiceInterceptor();
//    }

    /**
     * 添加拦截器
     *
     * @param registry
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        //管理后台拦截
        registry.addInterceptor(new AdminServiceInterceptor())
                .addPathPatterns("/admapi/user/**")//添加拦截器
                .addPathPatterns("/admapi/userAlbum/**")
                .addPathPatterns("/admapi/userList/**")
                .addPathPatterns("/admapi/userMusic/**")
                .addPathPatterns("/admapi/userMv/**")
                .addPathPatterns("/admapi/userSinger/**")
                .excludePathPatterns("/admapi/album/**")//对应的不拦截的请求
                .excludePathPatterns("/admapi/list/**")
                .excludePathPatterns("/admapi/userLogin/**")
                .excludePathPatterns("/admapi/music/**")
                .excludePathPatterns("/admapi/mv/**")
                .excludePathPatterns("/admapi/singer/**");
        registry.addInterceptor(new LoginInterceptor())
                .excludePathPatterns("/admapi/**")
                .addPathPatterns("/admapi/user/**");

    }

    /**
     * 需要重新指定静态资源，解决swagger与WebMvcConfigurationSupport冲突导致404的问题
     *
     * @param registry
     */
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/**").addResourceLocations("/");
        registry.addResourceHandler("/**").addResourceLocations("classpath:/static/");
        registry.addResourceHandler("doc.html").addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("/upload/**").addResourceLocations("file:D:/IDEA/musicc/3218005352-chenyan-server/doc/uploadfile/upload/");
        registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
        super.addResourceHandlers(registry);
    }

}
